package top.itbbn.otaifs;

import com.alibaba.fastjson2.JSONObject;
import org.apache.commons.codec.digest.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.security.MessageDigest;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Date;

public class OtaifsReq {
    public String signedKey = "4b44131d9965e6a3";
    public String method = "createPaymentOrder";
    public String user = "CESHI";


    //单例模式

    //私有化构造方法使得该类无法在外部通过new 进行实例化
    private OtaifsReq() {
    }

    //准备一个类属性，用于指向一个实例化对象，但是暂时指向null
    private static OtaifsReq instance;

    //public static 方法，返回实例对象
    public static OtaifsReq getInstance() {
        //第一次访问的时候，发现instance没有指向任何对象，这时实例化一个对象
        if (null == instance) {
            instance = new OtaifsReq();
        }
        //返回 instance指向的对象
        return instance;
    }


    /**
     * 获取 requestParam 参数
     */
    public String getRequestParam() {
        String xml = getXml();
        String base64Xml = Base64.getEncoder().encodeToString(xml.getBytes(StandardCharsets.UTF_8));
        String sign = getSign(signedKey, base64Xml);
        JSONObject requestParam = new JSONObject();
        requestParam.put("data", base64Xml);
        requestParam.put("signed", sign);
        requestParam.put("securityType", "MD5");
        return requestParam.toString();

    }

    /**
     * 获取方法
     *
     * @return
     */
    public String getMethod() {
        return method;
    }


    /**
     * 签名
     *
     * @param key
     * @param data
     * @return
     */
    public String getSign(String key, String data) {
        return encryptToMD5(key + data);
    }

    /**
     * MD5加密之方法一
     *
     * @param str 待加密字符串
     * @return 16进制加密字符串
     * @explain 借助apache工具类DigestUtils实现
     */
    private String encryptToMD5(String str) {
        return DigestUtils.md5Hex(str);
    }


    /**
     * 获取报文
     *
     * @return
     */
    public String getXml() {
        String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> <qm:request xmlns:qm=\"http://tour.ectrip.com/2014/QMRequestDataSchema\" xsi:schemaLocation=\"http://tour.ectrip.com/2014/QMRequestDataSchema QMRequestDataSchema-1.1.0.xsd\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"> <qm:header> <qm:application>tour.ectrip.com</qm:application> <qm:processor>DataExchangeProcessor</qm:processor> <qm:version>v2.0.0</qm:version> <qm:bodyType>CreateOrderPaymentRequestBody</qm:bodyType> <qm:createUser>cesh</qm:createUser> <qm:createTime>2022-07-16 06:29:25</qm:createTime> <qm:supplierIdentity>CESHI</qm:supplierIdentity> </qm:header> <qm:body xsi:type=\"qm:CreatePaymentOrderRequestBody\"> <qm:orderInfo> <qm:orderId>";
        Date date = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("YYYYMMddHHmmss_SSS_");
        String sn = "cs" + sdf.format(date);
        xml += sn + Math.round((Math.random() + 1) * 1000);
        xml += "</qm:orderId> <qm:product> <qm:resourceId>13</qm:resourceId> <qm:productName>测试成人票</qm:productName> <qm:visitDate>2022-07-16</qm:visitDate> <qm:endDate></qm:endDate> <qm:sellPrice>0</qm:sellPrice> </qm:product> <qm:contactPerson> <qm:name>测试</qm:name> <qm:mobile>18128918182</qm:mobile> <qm:credentials>440224199003132856</qm:credentials> <qm:credentialsType>ID_CARD</qm:credentialsType> </qm:contactPerson> <qm:visitPerson> <qm:person> <qm:name>测试</qm:name> <qm:mobile>18128918182</qm:mobile> <qm:credentials>440224199003132856</qm:credentials> <qm:credentialsType>ID_CARD</qm:credentialsType> </qm:person> </qm:visitPerson> <qm:orderQuantity>1</qm:orderQuantity> <qm:orderPrice>0</qm:orderPrice> <qm:orderStatus>PREPAY_ORDER_PRINTING</qm:orderStatus> </qm:orderInfo> </qm:body> </qm:request>";
        return xml;
    }


}
